package com.hui.config.yj;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

import java.util.Collections;

public class YjAuthenticationProvider implements AuthenticationProvider {


    /**
     * 认证逻辑
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String code = String.valueOf(authentication.getPrincipal());
        //调用认证中心认证

        if (!"121212".equals(code)) {
            throw new BadCredentialsException("Bad credentials");
        }

        //认证通过，返回新构建的token
        YjAuthenticationToken authenticationToken = new YjAuthenticationToken(code, Collections.emptySet());
        return authenticationToken;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return YjAuthenticationToken.class.isAssignableFrom(authentication);
    }


}
